<?php


namespace app\wechat\controller;


use app\common\controller\WechatUserBase;
use app\common\model\ForumCate;
use think\Db;

class Forum extends WechatUserBase
{
//    获取分类
    public function getForumCate(){
        $param = input();
        $data = ForumCate::where('show','=',10)->order(['sort'=>'desc'])->select();
        if (!empty($param['type']) && $param['type'] != 'form'){
            $data[count($data)] = [
                'id'=>'',
                'name'=>'全部',
                'img'=>'/uploads/20200607/d57b67048fcf93f543ab1f1b8f94d793.png',
            ];
        }

        foreach ($data as $k=>&$v){
            $v['img'] = getFullPath($v['img']);
        }
        $this->result($data);
    }
    //    获取店铺分类
    public function getForumCateswipe(){
        $data = ForumCate::where('show','=',10)->order(['sort'=>'desc'])->select();
        $data = $data->toArray();
        array_unshift($data,[
            'id'=>'',
            'name'=>'全部',
            'img'=>'/uploads/20200607/d57b67048fcf93f543ab1f1b8f94d793.png',
        ]);
        $cate = [];
        foreach ($data as $k=>&$v){
            $v['img'] = getFullPath($v['img']);
            $cate[ceil(($k+1)/8)-1][] = $v;
        }
        $this->result($cate);
    }
//    保存帖子
    public function saveforum(){
        $param = input();
        $forumModel = new \app\common\model\Forum();

        $param['forum_cate_id'] = Db::name('forum_cate')->where('name',$param['forum_cate'])->value('id');
        if (!empty($param['img'])){
            $param['img'] = implode(',',$param['img']);
        }
        if (!empty($param['video'])){
            $param['video'] = implode(',',$param['video']);
        }

        if (!empty($param['address'])){
            $address = explode(',',$param['address']);
            $param['province'] = $address[0];
            $param['city'] = $address[1];
            $param['area'] = $address[2];
        }
        $param['address'] = $param['address_detail'];

        if (!isset($param['id']) || empty($param['id'])){
            $param['user_id'] = session('wechat.id');
            $res = $forumModel->allowField(true)->isUpdate(false)->save($param);
        }else{
            $res = $forumModel->allowField(true)->isUpdate(true)->save($param,['id'=>$param['id']]);
        }

        if ($res !== false){
            $this->success();
        }else{
            $this->error();
        }
    }
//    获取帖子列表
    public function getForumList(){
        $param = input();
        $address = '';
        $where = [];

//        如果传过来user_id那么只能查询用户的，不需要定位查询
        if (!empty($param['user_id'])){
            $where[] = ['user_id','=',$param['user_id']];
        }else{
            $lat = $param['latitude'];
            $lng = $param['longitude'];
            if (!empty($lat) && is_numeric($lat) && !empty($lng) && is_numeric($lng)){
                $address = getAddress($param['latitude'],$param['longitude']);
            }else{
                $addressByIp = getAddressBtIp();
                if (empty($addressByIp)){
                    $this->error('获取位置失败，请打开手机定位');
                }
                $address['province'] = $addressByIp['ad_info']['province'];
                $address['city'] = $addressByIp['ad_info']['city'];
                $address['district'] = $addressByIp['ad_info']['district'];
            }

            if (empty($address)){
                $this->error('定位失败，请重新进入');
            }

//        只能查询本市区的
            $where[] = ['province','=',$address['province']];
            $where[] = ['city','=',$address['city']];
            $where[] = ['area','=',$address['district']];
        }

        if (!empty($param['keywords'])){
            $where[] = ['title','like','%'.$param['keywords'].'%'];
        }
        if (!empty($param['forum_cate_id'])){
            $where[] = ['forum_cate_id','=',$param['forum_cate_id']];
        }

//        只查看已发布的
        $where[] = ['state','=',10];
        $data = \app\common\model\Forum::with(['forumCate'])->where($where)
            ->order('top asc,recommend asc,id desc')->append(['desc'])->paginate($param['limit'],false,['query'=>$param]);
        foreach ($data as $k=>&$v){
            if (!empty($v['img'])){
                $v['img_thumb'] = explode(',',$v['img'])[0];
            }else{
                $v['img_thumb'] = '';
            }
        }
        $data = $data->toArray();
        return $this->result($data,$data['total'],'0',['address'=>$address]);
    }
//    根据经纬度获取地区
    public function getAddress(){
        $param = input();
        $lat = $param['latitude'];
        $lng = $param['longitude'];

        if (!empty($lat) && is_numeric($lat) && !empty($lng) && is_numeric($lng)){
            $address = getAddress($param['latitude'],$param['longitude']);
            $address['districtCode'] = Db::name('region')->where('cityName',$address['district'])->value('id');
        }else{
            $addressByIp = getAddressBtIp();
            if (empty($addressByIp)){
                $this->error('获取位置失败，请打开手机定位');
            }
            $address['province'] = $addressByIp['ad_info']['province'];
            $address['city'] = $addressByIp['ad_info']['city'];
            $address['district'] = $addressByIp['ad_info']['district'];
            $address['districtCode'] = Db::name('region')->where('cityName',$address['district'])->value('id');
        }
        $this->result($address);
    }

//    获取帖子详情
    public function getForumDetail(){
        $forum_id = input('forum_id');
        Db::name('forum')->where('id',$forum_id)->setInc('click_num');
        $data = \app\common\model\Forum::with(['forumCate','user'])->where('id',$forum_id)->append(['address_full'])->find();
        $this->result($data);
    }

    public function del(){
        $forum_id = input('forum_id');
        $res = \app\common\model\Forum::destroy($forum_id);
        if ($res !== false){
            $this->success();
        }else{
            $this->error();
        }
    }
}
